package com.itheima.mp.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mp.domain.po.User;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(description = "分页条件")
public class PageQuery {
    @ApiModelProperty("页码")
    private Long pageNo = 1L;
    @ApiModelProperty("每页条数")
    private Long pageSize = 5L;
    @ApiModelProperty("排序字段")
    private String sortBy;
    @ApiModelProperty("是否升序")
    private Boolean isAsc = true;

    public <T>Page<T> toMpPage(OrderItem...orderItems){
        //1.构造分页条件
        Page<T> page = Page.of(pageNo, pageSize);
        //1.1设置排序字段
        if(StrUtil.isNotBlank(sortBy)){
            page.addOrder(new OrderItem().setColumn(sortBy).setAsc(isAsc));
        }else{
            page.addOrder(orderItems);
        }
        return page;
    }

    public <T>Page<T> toMpPageSortByUpdateTime(){
        Page<T> page = toMpPage(OrderItem.desc("update_time"));
        return page;
    }

    public <T>Page<T> toMpPageSortByCreatTime(){
        Page<T> page = toMpPage(OrderItem.desc("creat_time"));
        return page;
    }
}
